<?php
namespace app\admin\model;
use think\Model;

class Goods extends Model{
	protected $field = true;

	protected static function init()
	{
		Goods::beforeInsert(function ($goods) {
			if($_FILES['og_thumb']['tmp_name']){
				$thumbName = $goods->upload('og_thumb');
				$ogThumb = date("Ymd").DS.$thumbName;
				$smThumb = date("Ymd").DS.'sm_'.$thumbName;
				$midThumb = date("Ymd").DS."mid_".$thumbName;
				$bigThumb = date("Ymd").DS."big_".$thumbName;
				$image = \think\Image::open(IMG_UPLOAD.$ogThumb);
				$image->thumb(500,500)->save(IMG_UPLOAD.$bigThumb);
				$image->thumb(100,100)->save(IMG_UPLOAD.$midThumb);
				$image->thumb(50,50)->save(IMG_UPLOAD.$smThumb);
				$goods->og_thumb=$ogThumb;
				$goods->big_thumb=$bigThumb;
				$goods->mid_thumb=$midThumb;
				$goods->sm_thumb=$smThumb;
			}
			$goods->goods_code = time().rand(000000,999999);
		});


		Goods::afterInsert(function ($goods) {
			$goodsId = $goods->id;
			$data = input('post.');			

			// 处理会员价格
			$memberPrice = db('member_price');
			if($data['member_price']){
				foreach($data['member_price'] as $k=>$v){
					if(trim($v)){
						$memberPrice->insert(['mprice'=>$v,'mlevel_id'=>$k,'goods_id'=>$goodsId]);
					}else{
						continue;
					}
				}
			}

			// 处理商品属性
			$i=0;
			if(isset($data['goods_attr'])){
				foreach($data['goods_attr'] as $k=>$v){
					if(is_array($v)){
						if(!empty($v)){
							foreach($v as $k1=>$v1){
								if(!$v1){
									$i++;
									continue;
								}
								db('goods_attr')->insert(['attr_id'=>$k,'attr_value'=>$v1,'attr_price'=>$data['attr_price'][$i],'goods_id'=>$goodsId]);
								$i++;
							}
						}
					}else{
						db('goods_attr')->insert(['attr_id'=>$k,'attr_value'=>$v,'goods_id'=>$goodsId]);
					}
				}
			}

			// 处理商品相册
			if($goods->_hasImgs($_FILES['goods_photo']['tmp_name'])){
				$files = request()->file('goods_photo');
				foreach($files as $file){
					$info = $file->move(ROOT_PATH.'public'.DS.'static'.DS.'upload');
					if($info){
						$photoName = $info->getFilename();
						$ogphoto=date("Ymd").DS.$photoName;
						$bigphoto=date("Ymd").DS.'big_'.$photoName;
						$midphoto=date("Ymd").DS.'mid_'.$photoName;
						$smphoto=date("Ymd").DS.'sm_'.$photoName;
						$image = \think\Image::open(IMG_UPLOAD.$ogphoto);
						$image->thumb(500,500)->save(IMG_UPLOAD.$bigphoto);
						$image->thumb(200,200)->save(IMG_UPLOAD.$midphoto);
						$image->thumb(80,80)->save(IMG_UPLOAD.$smphoto);
						db('goods_photo')->insert(['goods_id'=>$goodsId,'og_photo'=>$ogphoto,'big_photo'=>$bigphoto,'mid_photo'=>$midphoto,'sm_photo'=>$smphoto]);
					}else{
						echo $file->getError();
					}
				}
			}
		});

		// 商品删除
		Goods::beforeDelete(function ($goods){
			$goodsId = $goods->id;

			// 删除商品主图
			$thumb=array();
			$thumb[]=IMG_UPLOAD.$goods->og_thumb;
			$thumb[]=IMG_UPLOAD.$goods->big_thumb;
			$thumb[]=IMG_UPLOAD.$goods->mid_thumb;
			$thumb[]=IMG_UPLOAD.$goods->sm_thumb;
			foreach($thumb as $k=>$v){
				if(file_exists($v)){
					@unlink($v);
				}
			}

			// 删除会员价格
			db('member_price')->where(['goods_id'=>$goodsId])->delete();

			// 删除商品属性
			db('goods_attr')->where(['goods_id'=>$goodsId])->delete();

			// 删除商品相册
			$goodsPhotoRes=db('goods_photo')->where(['goods_id'=>$goodsId])->select();
			$goodsPhoto = [];
			foreach($goodsPhotoRes as $k=>$v){
				$goodsPhoto[] = IMG_UPLOAD.$v['og_photo'];
				$goodsPhoto[] = IMG_UPLOAD.$v['big_photo'];
				$goodsPhoto[] = IMG_UPLOAD.$v['mid_photo'];
				$goodsPhoto[] = IMG_UPLOAD.$v['sm_photo'];
				foreach($goodsPhoto as $k1=>$v1){
					if(file_exists($v1)){
						@unlink($v1);
					}
				}
			}

			db('goods_photo')->where(['goods_id'=>$goodsId])->delete();
			// $goodsPhotoRes = db('goods_photo')->where(['goods_id']=>$goodsId)->select();
		});



	}

	// 上传图片
    public function upload($imgName){
	    $file = request()->file($imgName);
	    if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'static'. DS . 'upload');
	        if($info){
	            return $info->getFilename(); 
	        }else{
	            echo $file->getError();
	        }
	    }
	}

	private function _hasImgs($tmpArr){
		foreach($tmpArr as $k => $v){
			if($v){
				return true;
			}
		}
		return false;
	}
	
}